package com.cj.mobile.iabp.mvp.ui.activity;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.di.component.DaggerModifyLoginPwdComponent;
import com.cj.mobile.iabp.di.module.ModifyLoginPwdModule;
import com.cj.mobile.iabp.mvp.contract.ModifyLoginPwdContract;
import com.cj.mobile.iabp.mvp.presenter.ModifyLoginPwdPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改密码
 */
public class ModifyLoginPwdActivity extends BaseActivity<ModifyLoginPwdPresenter> implements ModifyLoginPwdContract.View, TextWatcher {
    /*----------------控件信息----------------*/

    @BindView(R.id.edttxt_modifyloginpwdactivity_old_pwd)
    EditText edtOldPwd; // 原密码

    @BindView(R.id.edttxt_modifyloginpwdactivity_new_pwd)
    EditText edtNewPwd; // 新密码

    @BindView(R.id.edttxt_modifyloginpwdactivity_confirm_newpwd)
    EditText edtConfirmNewPwd; // 确认新密码

    @BindView(R.id.imgvi_modifyloginpwdactivity_password_close_icon)
    ImageView imgviPwdClose; // 清空原密码输入框

    @BindView(R.id.imgvi_modifyloginpwdactivity_password_see)
    ImageView imgviPwdsee; // 原密码可见

    @BindView(R.id.imgvi_modifyloginpwdactivity_newpwd_close_icon)
    ImageView imgviPwdClose2; // 清空新密码输入框

    @BindView(R.id.imgvi_modifyloginpwdactivity_newpwd_see)
    ImageView imgviPwdsee2; // 新密码密码可见

    @BindView(R.id.imgvi_modifyloginpwdactivity_confirm_newpwd_close_icon)
    ImageView imgviPwdClose3; // 清空新确认密码输入框

    @BindView(R.id.imgvi_modifyloginpwdactivity_confirm_newpwd_see)
    ImageView imgviPwdsee3; // 新确认密码密码可见

    @BindView(R.id.btn_modifyloginpwdactivity_submit)
    Button btnSubmit; // 提交按钮

    /*----------------业务信息----------------*/
    private Boolean isVisible = false;// 原密码是否可见
    private Boolean isVisible2 = false;// 新密码是否可见
    private Boolean isVisible3 = false;// 新确认密码是否可见
    /**
     * 对话框
     */
    private MaterialDialog mDialog;
    @Inject
    AccountManager accountManager;

    /**
     * 根据主题使用不同的颜色。
     * 如果想要纯透明，则需要重写此方法，返回值为 -1 即可。
     */
    public int useStatusBarColor() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(android.R.color.white, typedValue, true);
        return typedValue.data;
    }

    /**
     * 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
     */
    public boolean useLightStatusBar() {
        return true;
    }

    @Override
    protected void onDestroy() {
        KeyboardUtils.unregisterSoftInputChangedListener(getWindow());
        if (mDialog != null) {
            this.mDialog.dismiss();
        }
        super.onDestroy();
        this.mDialog = null;
        this.accountManager = null;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerModifyLoginPwdComponent
                .builder()
                .appComponent(appComponent)
                .modifyLoginPwdModule(new ModifyLoginPwdModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_modify_login_pwd;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        // 设置标题
        setTitle(R.string.modifyloginpwdactivity_modifyloginpwd);

        edtOldPwd.setTypeface(Typeface.DEFAULT);
        edtOldPwd.setTransformationMethod(new PasswordTransformationMethod());
        edtNewPwd.setTypeface(Typeface.DEFAULT);
        edtNewPwd.setTransformationMethod(new PasswordTransformationMethod());
        edtConfirmNewPwd.setTypeface(Typeface.DEFAULT);
        edtConfirmNewPwd.setTransformationMethod(new PasswordTransformationMethod());
        edtOldPwd.addTextChangedListener(this);
        edtNewPwd.addTextChangedListener(this);
        edtConfirmNewPwd.addTextChangedListener(this);

        // 初始化Loading对话框
        mDialog = new MaterialDialog.Builder(this).content(R.string.common_execute).progress(true, 0).build();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showLoading() {
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        mDialog.cancel();
    }

    /**
     * 点击事件
     */
    @OnClick({
            R.id.imgvi_modifyloginpwdactivity_password_close_icon,              //清空原密码输入框
            R.id.imgvi_modifyloginpwdactivity_password_see,                     //原密码可见
            R.id.imgvi_modifyloginpwdactivity_newpwd_close_icon,                //清空新密码输入框
            R.id.imgvi_modifyloginpwdactivity_newpwd_see,                       //新密码可见
            R.id.imgvi_modifyloginpwdactivity_confirm_newpwd_close_icon,        //清空确认密码输入框
            R.id.imgvi_modifyloginpwdactivity_confirm_newpwd_see,               //确认密码可见
            R.id.btn_modifyloginpwdactivity_submit                              //提交
    })
    @Override
    public void onClick(View v) {
        hideInput();
        switch (v.getId()) {
            case R.id.btn_modifyloginpwdactivity_submit:                        // 提交
                if (!CommonUtils.isDoubleClick()) {
                    String oldPwd = edtOldPwd.getText().toString().trim();
                    String newPwd = edtNewPwd.getText().toString().trim();
                    String confirmNewPwd = edtConfirmNewPwd.getText().toString().trim();
                    if (mPresenter != null) {
                        mPresenter.btnSubmitOnClick(oldPwd, newPwd, confirmNewPwd);
                    }
                }
                break;
            case R.id.imgvi_modifyloginpwdactivity_password_close_icon:         // 清空原密码输入框
                edtOldPwd.setText("");
                break;
            case R.id.imgvi_modifyloginpwdactivity_password_see:                // 原密码是否可见

                if (isVisible) {
                    isVisible = false;
                    imgviPwdsee.setBackgroundResource(R.mipmap.icon_statistics_see);
                    edtOldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    imgviPwdsee.setBackgroundResource(R.mipmap.icon_statistics_no_see);
                    isVisible = true;
                    edtOldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                edtOldPwd.setSelection(edtOldPwd.length());
                break;
            case R.id.imgvi_modifyloginpwdactivity_newpwd_close_icon:           // 清空确认密码输入框
                edtNewPwd.setText("");
                break;
            case R.id.imgvi_modifyloginpwdactivity_newpwd_see:                  // 确认密码是否可见

                if (isVisible2) {
                    isVisible2 = false;
                    imgviPwdsee2.setBackgroundResource(R.mipmap.icon_statistics_see);
                    edtNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    imgviPwdsee2.setBackgroundResource(R.mipmap.icon_statistics_no_see);
                    isVisible2 = true;
                    edtNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                edtNewPwd.setSelection(edtNewPwd.length());
                break;
            case R.id.imgvi_modifyloginpwdactivity_confirm_newpwd_close_icon:   // 清空确认密码输入框
                edtConfirmNewPwd.setText("");
                break;
            case R.id.imgvi_modifyloginpwdactivity_confirm_newpwd_see:          // 确认密码是否可见

                if (isVisible3) {
                    isVisible3 = false;
                    imgviPwdsee3.setBackgroundResource(R.mipmap.icon_statistics_see);
                    edtConfirmNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    imgviPwdsee3.setBackgroundResource(R.mipmap.icon_statistics_no_see);
                    isVisible3 = true;
                    edtConfirmNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                edtConfirmNewPwd.setSelection(edtConfirmNewPwd.length());
                break;
        }
    }

    // 隐藏软键盘
    private void hideInput() {
        KeyboardUtils.hideSoftInput(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        hideInput();
        return super.onTouchEvent(event);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(getApplicationContext(), message);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (edtOldPwd.getText().length() > 0) {
            imgviPwdClose.setVisibility(View.VISIBLE);
        } else {
            imgviPwdClose.setVisibility(View.GONE);
        }

        if (edtNewPwd.getText().length() > 0) {
            imgviPwdClose2.setVisibility(View.VISIBLE);
        } else {
            imgviPwdClose2.setVisibility(View.GONE);
        }
        if (edtConfirmNewPwd.getText().length() > 0) {
            imgviPwdClose3.setVisibility(View.VISIBLE);
        } else {
            imgviPwdClose3.setVisibility(View.GONE);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onUpdateSuccess() {
        showMessage(getString(R.string.modifyloginpwdactivity_update_succ_tips));

        //清除缓存
        accountManager.clearAccountInfo();

        // 关闭之前打开过的Activity
//        Setting.finishActivity(this.getClass().getName());

        //关闭窗口
        killMyself();

        Bundle bundle = new Bundle();
        bundle.putBoolean("isStartLogin", false);
        ActivityUtils.startActivity(bundle, LoginActivity.class);
    }

    @Override
    public void onUpdateError() {

    }
}
